Sessions = DB[:sessions] do
end

Nodes = DB[:nodes] do
  default_accessor :path
  
  relation(:parent) {Nodes[:id => @parent_id]}
  relation(:children) {Nodes.filter(:parent_id => @id)}
  relation(:subtree) {Nodes.filter(:path => /#{@path}\/.+/)}
  
  relation(:producer) {Sessions[@producer_id]}
  
  before_delete do
    if @producer_id
      Event.stop_track(self)
    end
    children.delete
  end
end

node = Nodes['/beta/icex4/r1028'] # same as:
node = Nodes[:path => '/beta/icex4/r1028']

node.path #=> '/beta/icex4/r1028'
node.class #=> #<Class:...>
(icex4 = node.parent) == Nodes['/beta/icex4']

icex4.children == Nodes.filter(:parent_id => icex4.id)

########################################################

class Node < Sequel::Record(:nodes)
  set_default_accessor :path
  
  # relation(:parent) {dataset[:id => @parent_id]}
  belongs_to :parent, :model => Node, :using => :parent_id
  # relation(:children) {dataset.filter(:parent_id => @id)}
  has_many :children, :model => Node, :using => :parent_id
  
  relation(:subtree) {dataset.filter(:path => /#{@path}\/.+/)}
end

node = Node['/beta/icex4/r1028']
node.class #=> Node

